#!/bin/bash
# substr.sh
#
# C-like substr utility

N_ARGS=3 # 3 arguments required

# A min number of argument is required; check for it
if [ $# -ne "$N_ARGS" ]; then
	echo "Usage: $0 OLD_PATTERN NEW_PATTERN <FILE_NAME>"
	exit 1
fi

old=$1
new=$2

# is the 3rd argument an existing file?
if [ -f "$3" ]; then
    filename=$3
else
	echo "File \"$3\" does not exist."
	exit 1
fi

# Just replaces the old string with the new one
#
# s               is the substitute command
# /some_word/     indicates address matching
# g (global flag) does the substitution everywhere in the file
sed -e "s/$old/$new/g" $filename



exit 0
